//
//  LeftViewController.swift
//  SwiftSideslipLikeQQ
//
//  Created by JohnLui on 15/4/11.
//  Copyright (c) 2015年 com.lvwenhan. All rights reserved.
//

import UIKit

// 侧滑菜单 View Controller
class LeftViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    let titlesDictionary = ["出行服务","基本资料","通知","设置","注销"]

    @IBOutlet weak var settingTableView: UITableView!
    @IBOutlet weak var avatarImageView: UIImageView!
    
    @IBOutlet weak var heightLayoutConstraintOfSettingTableView: NSLayoutConstraint!
    
    
    @IBAction func unloginbtnclick(sender: AnyObject) {
        let userloginfo = uiRealm.objects(loginInfo)
        try!uiRealm.write{
            uiRealm.delete(userloginfo)
        }
        let stb:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let loginview:LoginViewController = stb.instantiateViewControllerWithIdentifier("LoginView") as! LoginViewController
        self.presentViewController(loginview, animated: true,completion: nil)
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        settingTableView.delegate = self
        settingTableView.dataSource = self
        settingTableView.tableFooterView = UIView()
        
        heightLayoutConstraintOfSettingTableView.constant = Common.screenHeight < 500 ? Common.screenHeight * (568 - 221) / 568 : 347
        self.view.frame = CGRectMake(0, 0, 320 * 0.78, Common.screenHeight)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 处理点击事件
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let viewController = Common.rootViewController
        
        //选中项的内容
        let destinationStr = titlesDictionary[indexPath.row]
        
        switch destinationStr{
            case "我的任务":
                let mb=self.storyboard;
                let rv:UIViewController=(mb?.instantiateViewControllerWithIdentifier("CityDataView"))! as UIViewController
                //跳转
                self.presentViewController(rv, animated: true, completion: nil)
            case "机场信息":
                viewController.homeViewController.performSegueWithIdentifier("showCityInfoTo", sender: self)
                break
            case "出行服务":
                viewController.homeViewController.performSegueWithIdentifier("showTripView", sender: self)
                break
            case "通知":
                viewController.homeViewController.performSegueWithIdentifier("showCityList", sender: self)
                break
            case "设置":
                break
            case "注销":
                unlogin()
                break
        default:
            break
        }
        
        viewController.homeViewController.titleOfOtherPages = titlesDictionary[indexPath.row]
        //viewController.homeViewController.performSegueWithIdentifier("showOtherPages", sender: self)
        Common.chatVC.view.removeFromSuperview()
        viewController.mainTabBarController.tabBar.hidden = true
        viewController.mainTabBarController.selectedIndex = 0
        viewController.showHome()
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titlesDictionary.count
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("leftViewCell", forIndexPath: indexPath) 
        
        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel!.text = titlesDictionary[indexPath.row]
        
        return cell
    }
    
    func unlogin()
    {
        let userloginfo = uiRealm.objects(loginInfo)
        
        try!uiRealm.write{
            uiRealm.delete(userloginfo)
        }
        let stb:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let loginview:LoginViewController = stb.instantiateViewControllerWithIdentifier("LoginView") as! LoginViewController
        self.presentViewController(loginview, animated: true,completion: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
